﻿using System;
using System.IO;
using System.Linq;
using System.Windows;

namespace OrderEntryProject
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
	    private const Int32 PageSize = 25;
	    private Int32 _pageCount;
	    private Int32 _customersCount;
	    private IQueryable<Tuple<String, String>> _customers;
        private StringWriter _sw = new StringWriter();
	    private readonly NorthwindDataContext _context = new NorthwindDataContext();

		public MainWindow()
		{
			InitializeComponent();
			var context = new NorthwindDataContext();
			var sw = new StringWriter();
			context.Log = sw;

			#region inner joins

			//var customers = context.Customers.Join(context.Orders, c => c.CustomerID, o => o.CustomerID,
			//                                       (c, o) => new {c.CustomerID, c.CompanyName, o.OrderID, o.OrderDate})
			//                                       .OrderBy(r => r.CustomerID)
			//                                       .ThenBy(r => r.OrderID).Take(5);

			//var customers = from c in context.Customers
			//                join o in context.Orders
			//                    on c.CustomerID equals o.CustomerID
			//                orderby c.CustomerID, o.OrderID
			//                select new
			//                        {
			//                            c.CustomerID,
			//                            c.CompanyName,
			//                            o.OrderID,
			//                            o.OrderDate
			//                        };

			#endregion innerjoins

			#region outer joins

			//var customers = context.Customers.GroupJoin(context.Orders, c => c.CustomerID, o => o.CustomerID,
			//                                            (c, o) => new {c.CustomerID, c.CompanyName, Orders = o})
			//    .SelectMany(t => t.Orders.DefaultIfEmpty().Select(ord => new
			//                                                                {
			//                                                                    t.CustomerID,
			//                                                                    t.CompanyName,
			//                                                                    OrderID = (int?)ord.OrderID,
			//                                                                    OrderDate = (DateTime?)ord.OrderDate
			//                                                                }))
			//                                                                .OrderBy(t => t.CustomerID)
			//                                                                .ThenBy(t => t.OrderID);

			//var customers = from c in context.Customers
			//                join o in context.Orders
			//                    on c.CustomerID equals o.CustomerID into innerJoined
			//                from outerJoined in innerJoined.DefaultIfEmpty()
			//                orderby c.CustomerID , outerJoined.OrderID
			//                select new
			//                        {
			//                            c.CustomerID,
			//                            c.CompanyName,
			//                            OrderId = (int?)outerJoined.OrderID,
			//                            OrderDate = (DateTime?)outerJoined.OrderDate
			//                        };

			#endregion outer joins

			//dataGrid1.ItemsSource = customers;
			//MessageBox.Show(sw.GetStringBuilder().ToString());
		}

        //private void button1_Click(object sender, RoutedEventArgs e)
        //{
        //    #region grouping and joining

        //    //var context = new NorthwindDataContext();
        //    //    var sw = new StringWriter();
        //    //    context.Log = sw;

        //    //    var orders = from o in context.Order_Details
        //    //                 group o by o.OrderID
        //    //                 into grouped
        //    //                 select new
        //    //                            {
        //    //                                OrderId = grouped.Key,
        //    //                                OrderTotalAmount = grouped.Sum(o => (o.Quantity * o.UnitPrice *(1 - (Decimal)o.Discount)))
        //    //                            };
        //    //    dataGrid1.ItemsSource = orders;
        //    //    MessageBox.Show(sw.GetStringBuilder().ToString());

        //    #endregion

        //    #region paging

        //    var context = new NorthwindDataContext {Log = _sw};
        //    _customers = from c in context.Customers
        //                 orderby c.CompanyName
        //                 select new Tuple<string, string>(c.CustomerID, c.CompanyName);

        //    _customersCount = _customers.Count();
        //    _pageCount = _customersCount / PageSize;
        //    if (_pageCount * PageSize < _customersCount)
        //        _pageCount++;

        //    scrollBar1.Minimum = 0;
        //    scrollBar1.Maximum = _pageCount;
        //    scrollBar1.Visibility = Visibility.Visible;
        //    scrollBar1.SmallChange = 1;
        //    scrollBar1_Scroll(null, null);
            
        //    #endregion
        //}

        //private void scrollBar1_Scroll(object sender, System.Windows.Controls.Primitives.ScrollEventArgs e)
        //{
        //    var customersDisplay = from c in _customers
        //                           select new {c.Item1, c.Item2};
        //    dataGrid1.ItemsSource = customersDisplay.Skip((Int32) scrollBar1.Value*PageSize).Take(PageSize);
        //    MessageBox.Show(_sw.GetStringBuilder().ToString());
        //}

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Application.Current.Properties["Context"] = _context;
            //using (var context = new NorthwindDataContext())
            //{
            //    cmbCustomers.ItemsSource = from c in context.Customers
            //                               select new {c.CustomerID, c.CompanyName};
            //}
            cmbCustomers.ItemsSource = from c in _context.Customers
                                       //select new {c.CustomerID, c.CompanyName};
                                             select new Tuple<String, String>(c.CustomerID, c.CompanyName);
            cmbCustomers.DisplayMemberPath = "Item2";
        }

        private void mnuSave_Click(object sender, RoutedEventArgs e)
        {
            _context.SubmitChanges();
            MessageBox.Show("Saved");
            cmbCustomers_SelectionChanged(null, null);
        }

        private void mnuNewOrder_Click(object sender, RoutedEventArgs e)
        {
            var customer = (Tuple<String, String>) cmbCustomers.SelectedValue;
            var orderWindow = new OrderWindow() { CustomerId = customer.Item1};
            orderWindow.Show();
        }

        private void mnuExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void cmbCustomers_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            var curCustomer = (Tuple<String, String>) cmbCustomers.SelectedValue;
            if(curCustomer == null)
                return;
            lstOrders.ItemsSource = from o in _context.Orders
                        where o.CustomerID == curCustomer.Item1
                        select new
                                   {
                                       o.OrderID,
                                       o.OrderDate,
                                       o.RequiredDate
                                   };
        }
	}
}
